<?php

//------------------------
// 商品评论控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\GoodsComment as GoodsCommentModel;

use think\Db;

class GoodsComment extends AdminBase
{
    protected $GoodsCommentModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->GoodsCommentModel        = new GoodsCommentModel();

    }


    public function index(){
        $id = $this->request->param('id',0,'intval');

        $info = $this->GoodsCommentModel->where('goods_id',$id)->paginate(10);

        $this->assign('info',$info);
// halt($comments->total());
        return $this->fetch('index');
    }

     // 删除操作
    public function delete(){
        $id = $this->request->param('id',0,'intval');

        $where['id'] = array('eq',$id);

        $res = $this->GoodsCommentModel->where($where)->delete();

        if ($res == false) {
            $this->error($this->GoodsCommentModel->getError());
        } else{
            $this->success('删除成功');
        }
    }

    // 批量处理
    public function batchProc(){
        $data = $this->request->param();

        $ids = implode(',',$data['ids']);
        $map['id']  = ['in',$ids];
        $type = $data['type'];

        switch ($type) {
            case 'disable':
                $res = $this->GoodsCommentModel->where($map)->update(['status' => '0']);
                $act = '禁止';
                break;
            case 'enable':
                $res = $this->GoodsCommentModel->where($map)->update(['status' => '1']);
                $act = '允许';
                break;

            default:
                $this->error('非法操作');
                break;
        }

        if ($res === false) {
            $this->error($this->GoodsCommentModel->getError());
        } else {
            $this->success('批量'.$act.'成功');
        }

    }

    // 回复页面
    public function reply(){
        $id = $this->request->param('id',0,'intval');
        $this->assign('comment_id',$id);

        $info = $this->GoodsCommentModel->where('id',$id)->find()->toArray();

        $user = Db('User')->field('username')->where('id',$info['user_id'])->find();
        $logo = Db('UserDetails')->field('logo')->where('user_id',$info['user_id'])->find();

        $info['user_name'] = $user['username'];
        $info['logo'] = $logo['logo'];

        $this->assign('info',$info);

        $adminReply = $this->GoodsCommentModel->field('content,created_time')->where('parent_id',$id)->order('created_time asc')->select()->toArray();

        $this->assign('adminReply',$adminReply);

        return $this->fetch('reply');
    }

    // 回复处理
    public function replyProc(){
        $data = $this->request->param('');
 
        $validate_result = $this->validate($data, 'GoodsComment.reply');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $replyData = array(
            'parent_id'    => $data['comment_id'],
            'content'      => $data['content'],
            'created_time' => time(),
            'status'       => 1
        );

        $res = $this->GoodsCommentModel->save($replyData);

        if ($res === false) {
            $this->error($this->GoodsCommentModel->getError());
        } else {
            $this->success('评论回复成功');
        }
    }




}